package com.rongshu.view.userInfo.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Common;
import com.rongshu.utils.Constant;
import com.rongshu.utils.RxHelper;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.login.entity.VerificationCode;
import com.rongshu.view.userInfo.iview.ChangePhoneView;
import com.rongshu.view.userInfo.presenter.ChangePhonePresenter;
import com.widget.EditTextWithDel;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscriber;

public class ChangePhoneActivity extends BaseActivity<ChangePhonePresenter> implements ChangePhoneView {
    @BindView(R.id.txt_top_title)
    TextView txt_top_title;//标题
    @BindView(R.id.txt_current_bind_phone)
    TextView txt_current_bind_phone;
    @BindView(R.id.edtVerification)
    EditTextWithDel edtVerification;
    @BindView(R.id.btn_next_step)
    TextView btn_next_step;
    @BindView(R.id.txtGetVerification)
    TextView txtGetVerification;

    //phone
    private String phone = null;
    //boolean
    private boolean is_has_verification_code = false;
    //request
    private final int CHANGE_PHONE_REQUEST_CODE = 0x03;//更换手机号
    //倒计时时间
    private int sumTimes = 60;
    //map
    Map<String, Object> map = null;
    //code
    private int code = -1;

    @Override
    protected int initLayout() {
        return R.layout.activity_change_phone_view;
    }

    @Override
    protected void initView() {
        //set presenter
        setPresenter(new ChangePhonePresenter(this), this);
        //设置title
        txt_top_title.setText(R.string.change_phone);
        //获取意图中数据
        phone = getIntent().getStringExtra(Constant.INTENT_MESSAGE);
        txt_current_bind_phone.setText(getString(R.string.current_bind_phone) + phone);
    }

    @Override
    protected void initData() {
        edtVerification.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    is_has_verification_code = true;
                } else {
                    is_has_verification_code = false;
                }
                btn_next_step.setBackgroundResource((is_has_verification_code) ?
                        R.drawable.shape_btn_3cc051 : R.drawable.shape_btn_e5e5e5);
            }
        });
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @OnClick({R.id.layout_back, R.id.btn_next_step, R.id.txtGetVerification})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_back:
                finish();
                break;
            case R.id.txtGetVerification://获取验证码
                getPhoneVerficationCode();
                countdown();
                break;
            case R.id.btn_next_step:
                String code = edtVerification.getText().toString().trim();
                if (TextUtils.isEmpty(code) || this.code == -1) {
                    return;
                }
                Intent intent = new Intent(this, BindPhoneActivity.class);
                intent.putExtra(Constant.INTENT_TITLE, getString(R.string.change_phone));
                intent.putExtra(Constant.INTENT_TYPE, 1);
                startActivityForResult(intent, CHANGE_PHONE_REQUEST_CODE);
                break;
        }
    }

    /**
     * 倒计时
     */
    private void countdown() {
        //倒计时操作
        RxHelper.countdown(sumTimes)
                .subscribe(new Subscriber<Integer>() {
                    @Override
                    public void onCompleted() {
                        txtGetVerification.setText(R.string.txt_get_verification_string);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(Integer integer) {
                        txtGetVerification.setText(integer + "s");
                    }
                });
    }

    /**
     * 获取验证码
     */
    private void getPhoneVerficationCode() {
        map = new HashMap<>();
        map.put("mobile", phone);
        map.put("type", 4);//1 APP验证码登录，2 APP忘记密码-重置密码，3 APP手机绑定，4 APP更换手机号码
        presenter.getPhoneVerficationCode(
                (String) map.get("mobile"),
                (int) map.get("type"),
                Common.encryptionSign(map));
        map.clear();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case CHANGE_PHONE_REQUEST_CODE:
                    //把最新的手机号返还
                    setResult(RESULT_OK, data);
                    finish();
                    break;
            }
        }
    }

    /**
     * 获取验证码回调
     */
    @Override
    public void getVerificationResult(VerificationCode verificationCode) {
        if (verificationCode.getErrCode() == 0) {
            code = verificationCode.getErrCode();
        } else {
            toast(verificationCode.getErrMsg());
        }
    }
}
